#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "numbers.h"
#include <QDebug>
#include <QString>
#include <QTextStream>
#include <QMessageBox>
#include <QFileDialog>
#include <QFile>

/////////////////////////////////////////////////////////////
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
   , ui(new Ui::MainWindow)
{
   ui->setupUi(this);

   connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(close()));
   connect(ui->openFileButton, SIGNAL(clicked()), this, SLOT(OnOpen()));

   DecimalNumbers = GetDecimalValues();
}

/////////////////////////////////////////////////////////////
MainWindow::~MainWindow()
{
   delete ui;
   ui = NULL;
}

/////////////////////////////////////////////////////////////
void MainWindow::OnOpen()
{
   QString fileName = QFileDialog::getOpenFileName(this, tr("Open account file"),
                                                      qApp->applicationDirPath(),
                                                      tr("Text (*.txt);;All files (*.*)"));

   ParseFile(fileName);
}

/////////////////////////////////////////////////////////////
void MainWindow::BuildSevenSegments(const QStringList& list)
{
   QVector<int> accountNumber;

   for(int i=0; i<list.size(); ++i)
   {
       if(i%3 == 0)
       {
           for(int j=0; j<list.at(i).size(); ++j)
           {
               if(j%3 == 0)
               {
                  QChar x = list[i][j];
                  QChar a = list[i][j+1];
                  QChar y = list[i][j+2];

                  QChar f = list[i+1][j];
                  QChar g = list[i+1][j+1];
                  QChar b = list[i+1][j+2];

                  QChar e = list[i+2][j];
                  QChar d = list[i+2][j+1];
                  QChar c = list[i+2][j+2];

                  QString segments = QString("%1%2%3\n%4%5%6\n%7%8%9")
                          .arg(x).arg(a).arg(y)
                          .arg(f).arg(g).arg(b)
                          .arg(e).arg(d).arg(c);

                  accountNumber.append(DecimalNumbers.value(segments));

                  if(accountNumber.size() == 9)
                  {
                     AccountNumbers.append(accountNumber);
                     accountNumber.clear();
                  }
               }
           }
       }
   }
}

/////////////////////////////////////////////////////////////
void MainWindow::ParseFile(const QString& fileName)
{
   if(!fileName.isEmpty())
   {
      ui->textBrowser->clear();

      AccountFile = fileName;
      QFile file(AccountFile);

      file.open(QIODevice::ReadOnly | QIODevice::Text);
      QTextStream in(&file);
      QStringList list;

      while(!in.atEnd())
      {
          QString line = in.readLine();
          list.append(line);
      }

      file.close();

      // build number
      BuildSevenSegments(list);
      ValidateAccountNumbers();
   }
}

/////////////////////////////////////////////////////////////
void MainWindow::ValidateAccountNumbers()
{
   // Valid account checksum: (d1+2*d2+3*d3 +..+9*d9) mod 11 = 0
   bool retVal = false;

   foreach(QVector<int> accountNumber, AccountNumbers)
   {
      QString number;
      retVal = ((9*accountNumber[0]+
                8*accountNumber[1]+
                7*accountNumber[2]+
                7*accountNumber[3]+
                6*accountNumber[4]+
                5*accountNumber[5]+
                4*accountNumber[5]+
                3*accountNumber[6]+
                2*accountNumber[7]+
                accountNumber[8]) % 11 == 0);

      foreach(int digit, accountNumber)
      {
         number.append(QString::number(digit));
      }

      ui->textBrowser->append(retVal ? number.append("  : Valid") : number.append("  : Invalid"));
   }
}
